package com.rosiminc.pm.game.tiles;

import com.rosiminc.pm.game.Tile;

/**
 * This object represents a flip tile.
 * 
 * @author Shymon
 * 
 */
public class FlipTile extends GameBoardTile {

	private Tile tile;
	private Tile tile2;
	private boolean firstTile;

	/**
	 * This constructor initialises the tile with given row, column and pipe
	 * tile.
	 * 
	 * @param row
	 *            the row
	 * @param col
	 *            the column
	 * @param tile
	 *            the pipe tile
	 * @throws IllegalArgumentException
	 *             if the tile is null
	 */
	public FlipTile(int row, int col, Tile tile) {
		this(row, col, tile, null);
	}

	/**
	 * This constructor initialises the tile with given row, column, pipe tile
	 * and secondary pipe tile.
	 * 
	 * @param row
	 *            the row
	 * @param col
	 *            the column
	 * @param tile
	 *            the pipe tile
	 * @throws IllegalArgumentException
	 *             if the tiles sent are null
	 */
	private FlipTile(int row, int col, Tile tile, Tile tile2) {
		super(row, col);

		setTile(tile);
		setTile2(tile2);
		this.firstTile = true;
	}

	/**
	 * This method sets the primary tile.
	 * 
	 * @param tile
	 *            the primary pipe tile
	 * @throws IllegalArgumentException
	 *             if tile is null
	 */
	private void setTile(Tile tile) {

		if (tile == null)
			throw new IllegalArgumentException("Null tile sent");
		this.tile = tile;
	}

	/**
	 * This method sets the secondary tile.
	 * 
	 * @param tile
	 *            the secondary pipe tile
	 * @throws IllegalArgumentException
	 *             if tile is null
	 */
	private void setTile2(Tile tile2) {

		if (tile2 != null)
			this.tile2 = tile2;
		else
			this.tile2 = generateTile(this.tile);
	}

	/**
	 * This method generates a random secondary tile.
	 * 
	 * @param original
	 *            the original tile
	 * @return the generated tile
	 */
	private Tile generateTile(Tile original) {
		Tile aTile;
		boolean invalid = true;

		do {
			aTile = Tile.getTile((int) (Math.random() * 15)+1);
			invalid = (aTile.getNumSides() == original.getNumSides());
		} while (invalid);

		return aTile;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.rosiminc.pm.game.tiles.GameBoardTile#actionA()
	 */
	@Override
	public void actionA() {
		firstTile = !firstTile;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.rosiminc.pm.game.tiles.GameBoardTile#actionB()
	 */
	@Override
	public void actionB() {
		firstTile = !firstTile;

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.rosiminc.pm.game.tiles.GameBoardTile#shuffle()
	 */
	@Override
	public void shuffle() {
		firstTile = ((int) (Math.random() * 2)) == 1;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.rosiminc.pm.game.tiles.GameBoardTile#getTile()
	 */
	@Override
	public Tile getTile() {
		if (firstTile)
			return tile;
		else
			return tile2;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.rosiminc.pm.game.tiles.GameBoardTile#getTileType()
	 */
	@Override
	public TileType getTileType() {
		return TileType.FLIP;
	}

}
